<?php
namespace curl;

class Curlrequest
{

    /**
     * @desc get请求
     * @param $url
     * @param int $timeOut
     * @param array $header
     * @return mixed
     */
    public static function get($url, $header = array(), $timeOut = 20)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
        curl_setopt($ch, CURLOPT_HEADER, false);
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        if (1 == strpos('$' . $url, 'https://')) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result = curl_exec($ch);

        //curl错误
        $curlError = curl_errno($ch);
        //关闭curl
        curl_close($ch);

        //如果有错误就返回错误
        if($curlError) {
            return $curlError;
        }
        return $result;
    }

    /**
     * post请求
     * @param $url
     * @param $params
     * @param array $header
     * @param int $timeOut
     * @return mixed
     */
    public static function post($url, $params = [], $header = array(), $timeOut = 20)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeOut);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if ($header) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        if (1 == strpos('$' . $url, 'https://')) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        $result = curl_exec($curl);

        //curl错误
        $curlError = curl_errno($curl);
        //关闭curl
        curl_close($curl);

        //如果有错误就返回错误
        if($curlError) {
            return $curlError;
        }
        return $result;
    }


    /**
     * @desc 发送json数据
     * @param string $url 发送地址
     * @param string $jsonString
     * @param int $timeOut
     * @return bool|int|string
     */
    public static function curlPostJson($url,$jsonString,$timeOut = 20)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonString)
            )
        );
        if (1 == strpos('$' . $url, 'https://')) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result = curl_exec($ch);

        //返回curl错误
        $curlError = curl_errno($ch);
        //关闭curl
        curl_close($ch);

        //如果有错误就返回错误
        if($curlError) {
            return $curlError;
        }
        return $result;
    }
}